home *** CD-ROM | disk | FTP | other *** search
- /**\
- |**| =====================================================================
- |**|
- |**| Glyph Layout.c
- |**|
- |**| This file contains the calls that this sample needs to make
- |**| the QuickDraw GX shell work correctly.
- |**|
- |**| QuickDraw GX Libraries Used:
- |**| "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
- |**| "LayoutLibrary.c", "ShapeLibrary.c", and "TransformLibrary.c".
- |**|
- |**| 6/96 bob Updated #includes to support changed GX Library names.
- |**| Updated the copyright date.
- |**|
- |**| ©1990 - 1996 Apple Computer, Inc.
- |**| All rights reserved.
- |**|
- |**| =====================================================================
- \**/
-
-
- #include "QDGX shell.h"
- #include <GXLayout.h>
- #include "LayoutLibrary.h"
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| PROTOTYPES
- |**| ---------------------------------------------------------------------
- \**/
-
- // funtions required by shell
-
- void DoSetup (void);
- void DoDraw (WindowPtr wind, Boolean updating);
- OSErr DoCreateNew (void);
- void DoDispose (WindowPtr wind);
- void DoIdle (WindowPtr wind);
- void DoTeardown (void);
- void DoClick (WindowPtr wind, Point p);
-
- // private functions
-
- OSErr DoWindowInit (WindowPtr wind);
- void CreateSampleImage (WindowPtr wind);
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| ENUMS
- |**| ---------------------------------------------------------------------
- \**/
- enum { rWindResource = 128 };
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| GLOBALS
- |**| ---------------------------------------------------------------------
- \**/
- // If gDebugging = TRUE, graphics library errors and notices will be posted. This
- // functionality will only work with the "debugging" version of QuickDraw GX.
- // If the debugging version is not installed, nothing bad will happen, but these
- // functions will not work.
-
- Boolean gDebugging = true;
-
- // Set "gGiveMeValidation" to TRUE if you want receive run-time validation.
-
- Boolean gGiveMeValidation = true;
-
-
- // gGraphicsHeapSize sets the size of the graphics heap created by calling the
- // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c. You can determine
- // the amount of graphics heap required by using GraphicsBug. I'm giving it 600K,
- // since we need abunch if we have several windows open.
-
- long gGraphicsHeapSize = 600;
-
- // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
- // override. This is so that our override can be native PowerPC code if necessary.
-
- GXPrintingEventUPP gOurPrintingOverrideUPP;
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoSetup()
- |**| Here's where we initialize any global variables our application needs.
- |**| We have only one at this time -- the universal proc pointer for
- |**| our printing override.
- |**| ---------------------------------------------------------------------
- \**/
- void DoSetup (void)
- { // Initialize our printing event override UPP
- gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoDraw()
- |**| Draw the contents of the window. The first parameter is the window
- |**| to draw, and the second parameter is true if we're updating an existing
- |**| image. If that's the case, we don't want to change anything, but
- |**| just draw what's already there.
- |**| ---------------------------------------------------------------------
- \**/
- void DoDraw (WindowPtr wind, Boolean updating)
- {
- #pragma unused (updating)
- GXDrawShape (GetDocShape(wind));
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoCreateNew()
- |**| This routine is called when a window needs to be created.
- |**| ---------------------------------------------------------------------
- \**/
- OSErr DoCreateNew (void)
- {
- OSErr err = noErr;
- WindowPtr wind;
-
- // Get and create our window from the resource fork
-
- wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
-
- // Attach a default gxViewPort to it, create and iInitialize our
- // private data for it, and add a sample image to its page shape.
-
- if ( wind == NULL )
- return (MemError());
-
- GXIgnoreGraphicsNotice(transform_already_set);
- SetDefaultViewPort(GXNewWindowViewPort(wind));
- GXPopGraphicsNotice();
-
- err = DoWindowInit(wind);
- if ( err != noErr )
- return err;
-
- CreateSampleImage(wind);
- return err;
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoDispose()
- |**| This routine is called when a window needs to be disposed of.
- |**| ---------------------------------------------------------------------
- \**/
- void DoDispose (WindowPtr wind)
- {
- TH_Doc doc;
-
- // You should always dispose of your GX graphics objects before tossing your window.
- // Why? It's generally good form and this approach guarantees that everything is
- // disposed. If you had not disposed of everything, the call to DisposeWindow should
- // dispose of the objects. If you are running the debugging version of QuickDraw GX
- // with notices set, you will receive a notice that you had not disposed of everything.
- // You can turn notices on in this file by setting gDebugging = TRUE (above).
-
- if ( wind != NULL )
- {
- doc = (TH_Doc)GetWRefCon(wind); // Remember, this is where we stored our private data.
- GXDisposeShape(GetDocShape(wind)); // Dispose of this doc's shape.
- GXDisposeJob(GetDocJob(wind)); // Dispose of this doc's print job.
- DisposHandle((Handle) doc); // Dispose of our private data.
- DisposeWindow(wind); // Dispose of the window.
- }
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoIdle()
- |**| This routine is called to do things while idling through the event loop.
- |**| ---------------------------------------------------------------------
- \**/
- void DoIdle (WindowPtr wind)
- {
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoTeardown()
- |**| This routine is called just before we quit to remove anything
- |**| persistent that might have been setup by DoSetup().
- |**| ---------------------------------------------------------------------
- \**/
- void DoTeardown (void)
- {
- DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoClick()
- |**| ---------------------------------------------------------------------
- \**/
- void DoClick(WindowPtr window, Point p)
- {
- }
-
-
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoWindowInit()
- |**| In this function we create and initialize the the private document
- |**| structure for a new window. This structure contains the print job and
- |**| the shape which is drawn in the window. We store this data in a handle
- |**| and hang it off the window's refCon field for easy retrieval. By doing
- |**| this, rather than using globals, we can create many windows containing
- |**| unique print jobs and shapes.
- |**| ---------------------------------------------------------------------
- \**/
- OSErr DoWindowInit (WindowPtr wind)
- {
- OSErr err = noErr;
- gxJob docJob;
- gxShape docPage;
- TH_Doc windDoc;
-
-
- // Create the page shape. We set the unique items attribute to make sure that each item
- // added to the picture has a unique reference. If this attribute was not set, we would
- // not see all copies of anything we add to the shape multiple times -- we'd just see
- // the last version added.
-
- docPage = GXNewShape(gxPictureType);
- GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
-
-
- // Create a print job for this document. This will be the same as the system default until
- // the user goes through the dialogs for Page Setup or Print…
-
- err = GXNewJob(&docJob);
-
-
- // If there are no errors, create a handle the size of our document structure and store
- // the print job and page shape in it. Store the handle in the window's refCon field so
- // that we can get at it. (Note that the utility routines "GetDocJob" and "GetDocShape"
- // can be used to do this easily.
-
- if ( err == noErr )
- {
- windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
-
- if ( windDoc == NULL )
- err = MemError();
- else
- {
- (*windDoc)->docJob = docJob;
- (*windDoc)->docPage = docPage;
- SetWRefCon(wind, (long) windDoc);
- }
-
- // Now install our application override for PrintingEvent so that we can
- // support the new movable-modal printing dialog boxes.
-
- GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
-
- }
-
- return err;
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| CreateSampleImage()
- |**| This function creates primitive shapes and adds them to the window's page shape.
- |**| ---------------------------------------------------------------------
- \**/
- void CreateSampleImage (WindowPtr wind)
- {
- Rect ourWindowRect = wind->portRect; // the rectangle for this window
- // in QuickDraw coordinates
-
- gxRunControls runControls; // a default run controls structure
-
- gxShape layout; // the layout shape we're creating
- gxShape glyphs; // the glyph shape holding glyphs from our layout
- gxShape thePage; // this window's page shape
- gxLayoutOptions layoutOptions; // a default layout options structure
- gxStyle glyphStyles[3]; // an array of styles to use building our layout
- char * text1 = "Aetna "; // text of the first run
-
- // The text of the second run is "Arabic Macintosh" in Arabic: */
- // meem, alif, kaf, noon, tah, wau, shin, <sp>, alif, lam, ein, reh, beh, yeh
-
- static char text2[] = {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4,
- 0x20, 0xC7, 0xE4, 0xD9, 0xD1, 0xC8, 0xEA, 0};
-
- char * text3 = " Office AWAY."; // the text of the third run
- char * textRuns[3]; // array to hold the three text runs
- short textLengths[3]; // array to hold the lengths of each run
- short totalLength; // the length of all runs together
- gxPoint posn; // position of the layout shape
- gxGlyphcode layoutGlyphs[35]; // array to hold glyph codes of glyphs from layout
- gxPoint layoutPositions[35]; // array to hold locations of glyphs from layout
- gxPoint layoutTangents[35]; // array to hold tangents of glyphs from layout
- long layoutGlyphCount; // the count of glyphs in the layout
- long layoutAdvance[2]; // an array of advance bits for glyphs from layout
- long layoutRuns; // the number of style runs found in the layout
- short layoutRunCounts[3]; // array of number of glyphs in each style run
- short level0 = 0; // a variable to hold zero (we need its address)
- gxStyle layoutStyles[3]; // array to receive styles found in layout shape
-
- // Initialize the array of text runs for our layout shape
-
- textRuns[0] = text1;
- textRuns[1] = text2;
- textRuns[2] = text3;
-
- // Initialize the text lengths.
-
- textLengths[0] = MyStrLength(text1);
- textLengths[1] = MyStrLength(text2);
- textLengths[2] = MyStrLength(text3);
-
- totalLength = textLengths[0] + textLengths[1] + textLengths[2];
-
- // Use library routines to initialize default gxLayoutOptions and gxRunControls structures
-
- InitializeLayoutOptions (&layoutOptions);
- InitializeRunControls (&runControls);
-
- // Position the layout almost half way down the left edge of the window. Set
- // the layout's width to the window's width and set the flushness to 1/2, this
- // will cause the layout to center in the window. Note that ourWindowRect is
- // not in fixed coordinates, so we have to make it fixed to use it.
-
- layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
- layoutOptions.flush = fract1/2;
-
- posn.x = 0;
- posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2) - ff(25);
-
-
- // Use the library routines to initialize three style runs. The first one is 38-point
- // Helvetica. NewLayoutStyle is found in layout library.c.
-
- glyphStyles[0] = NewLayoutStyle(
- (char *) "\pHelvetica", // the gxFontName for the style
- ff(38), // the point size in fixed point
- 0, // gxTextAttributes (none)
- &runControls, // our default run controls
- nil, // run features (none for us)
- 0, // count of run features
- nil); // style run overrides (none for us)
-
- // The second one is 38-point Baghdad.
-
- glyphStyles[1] = NewLayoutStyle(
- (char *) "\pBaghdad Plain", // the gxFontName for the style
- ff(38), // the point size in fixed format
- 0, // gxTextAttributes (none)
- &runControls, // our default run controls
- nil, // run featuers (none for this style)
- 0, // count of run features
- nil); // style run overrides (none for us)
-
- // The third and final one is 38-point Times Roman
-
- glyphStyles[2] = NewLayoutStyle(
- (char *) "\pTimes Roman", // the gxFontName for this style
- ff(38), // the point size (fixed format)
- 0, // no gxTextAttributes
- &runControls, // our default run controls
- nil, // no run features
- 0, // count of no run features = 0
- nil); // style run overrides (still none)
-
- // We're ready to build our layout now!
-
- layout = GXNewLayout(
- 3, // count of text runs
- textLengths, // array of lengths of each run
- (void *)textRuns, // array of pointers to the text
- 3, // count of style runs
- textLengths, // array of the byte lengths of each run
- glyphStyles, // array of the styles
- 1, // number of levels in the layout
- &totalLength, // array of lengths of levels (only one)
- &level0, // array of the levels (only one)
- &layoutOptions, // options (default)
- &posn); // position of the layout shape (none)
-
- // Now that we have a layout shape, we show how to get glyphs back from it.
- // We can do this several ways. One easy way is to call GXPrimitiveShape on
- // the layout shape, which changes it permanently to a glyph shape. Of course,
- // then you can't change it back to a layout. A non-destructive way is to call
- // GXGetLayoutGlyphs, which gives us back a glyph shape as well as all kinds of
- // wonderfully useful information about the layout shape's geometry.
-
- GXIgnoreGraphicsNotice(glyph_tangents_have_no_effect); // we don't care
- layoutGlyphCount = GXGetLayoutGlyphs(
- layout, // the layout to get glyphs from
- layoutGlyphs, // an array of glyph codes we're getting
- layoutPositions, // the positions of each glyph in the array
- layoutAdvance, // the advance bits for all the glyphs
- layoutTangents, // the tangents for all the glyphs
- &layoutRuns, // the number of style runs in the layout
- layoutRunCounts, // the number of glyphs in each style run
- layoutStyles); // the styles from the layout shape
- GXPopGraphicsNotice();
-
- // Now we're going to make a new glyph shape that reflects all this information.
- // After creating a new glyph shape, we set the gxIgnorePlatformShape attribute
- // bit on it. Why? That tells QuickDraw GX to interpret the "text" we're sending
- // to layout not as ASCII or any other platform or script, but instead as actual
- // glyph offsets, which is what we got back from GXGetLayoutGlyphs. Then we're
- // ready to stuff our shape full of the glyph information we got. Remember that
- // a single glyph shape can draw multiple glyphs.
-
- glyphs = GXNewShape(gxGlyphType);
- GXSetShapeAttributes(glyphs, GXGetShapeAttributes(glyphs) | gxIgnorePlatformShape);
-
- GXSetGlyphs(
- glyphs, // the glyph shape to set
- layoutGlyphCount, // the count of glyphs we're setting in
- (unsigned char *) layoutGlyphs, // the glyph codes (the "text")
- layoutPositions, // the positions of each glyph
- layoutAdvance, // the advance bits for each glyph
- layoutTangents, // the tangents for each glyph
- layoutRunCounts, // the number of glyphs in each style run
- layoutStyles); // the style runs
-
- // Move the new shape down vertically 50 pixels so it doesn't overlap the layout shape.
- // Since we're using the same positions we got from the layout shape, failure to move
- // the new glyph shape would make them draw right on top of each other.
-
- GXMoveShape(glyphs, 0, ff(50));
-
- // Get this window's page shape and add our layout and glyph shapes to it.
-
- thePage = GetDocShape(wind);
-
- AddToShape(thePage, layout);
- AddToShape(thePage, glyphs);
-
- // Dispose of all the stuff we created
-
- GXDisposeShape(layout);
- GXDisposeShape(glyphs);
- GXDisposeStyle(glyphStyles[0]);
- GXDisposeStyle(glyphStyles[1]);
- GXDisposeStyle(glyphStyles[2]);
-
-
- // Invalidate the window's portRect so that everything gets updated.
-
- SetPort(wind);
- InvalRect(&ourWindowRect);
- }
-
-